package me.zhengjie.modules.sms.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.common.collect.Lists;

import java.util.List;

public class SmsUtil {

    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    static final String accessKeyId = "LTAI4G3WGCjv2kfYD2bUGooo";
    static final String accessKeySecret = "pEmlc7vie8OTIoNdZoq5WD672Z3wVP";

    public static CommonResponse sendSms(String objectId, String phoneNumber, String signName, String templateCode, String jsonContent) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", jsonContent);
        request.putQueryParameter("OutId", objectId);
        CommonResponse sendSmsResponse = acsClient.getCommonResponse(request);

        return sendSmsResponse;
    }

    public static CommonResponse sendBatchSms(List<String> phoneList, String signName, String templateCode, String jsonContent) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendBatchSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        request.putQueryParameter("PhoneNumberJson", pinStr(phoneList));
        request.putQueryParameter("SignNameJson", pinStr(copypar(phoneList, signName)));
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParamJson", jsonContent);
        //hint 此处可能会抛出异常，注意catch
        CommonResponse sendSmsResponse = acsClient.getCommonResponse(request);

        return sendSmsResponse;
    }

    public static String pinStr(List<String> valve) {
        StringBuilder str = new StringBuilder("[");
        valve.forEach(k ->
                str.append("\"").append(k).append("\"").append(",")
        );
        str.append("]");
        return str.delete(str.length() - 2, str.length() - 1).toString();
    }

    private static List copypar(List phoneJson, Object param) {
        List arrayList = Lists.newArrayList();
        phoneJson.forEach(k -> arrayList.add(param));
        return arrayList;
    }

}
